﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyTimeLine
{
   
    public delegate void GetDateDeleget(object obj);

    /// <summary>
    /// TimeLine1.xaml 的交互逻辑
    /// </summary>
    public partial class TimeLine : UserControl
    {
        public event GetDateDeleget GetDateListEvent;


        #region 属性
        public static ObservableCollection<Date> monthList;
        public static ObservableCollection<Date> MonthList
        {
            get { return monthList; }
            set
            {
                monthList = value;

            }
        }

        public ObservableCollection<Date> yearList;
        public ObservableCollection<Date> YearList
        {
            get { return yearList; }
            set
            {
                yearList = value;
            }
        }

        public int YearNow
        { get; set; }

        public int YearWidth
        { get; set; }

        public int MonthWidth
        { get; set; }

        public double _Width
        { get; set; }

        #endregion

        #region 初始化方法
        public TimeLine()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            InitData();
            listView_Years.AddHandler(ListView.MouseWheelEvent, new MouseWheelEventHandler(List_MouseWheel), true);
        }

        #endregion

        #region ListView横向滚动
        public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
                    T childItem = FindVisualChild<T>(child);
                    if (childItem != null)
                        return childItem;
                }
            }
            return null;
        }
        /// <summary>
        /// 滚轮响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void List_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ItemsControl items = (ItemsControl)sender;
            ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
            if (scroll != null)
            {

                int d = e.Delta;
                if (d > 0)
                {
                    scroll.LineRight();
                    this.LeftCanvas.Visibility = 0;
                    this.LeftCanvas.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF414141"));
                    this.RightCanvas.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("DarkOrange"));
                    double d1 = scroll.HorizontalOffset;
                    double d2 = scroll.ViewportWidth;
                    double d3 = scroll.ExtentWidth;
                    if (d1 + d2 == d3)
                    {
                        this.RightCanvas.Visibility = (Visibility)1;
                    }
                }
                if (d < 0)
                {
                    scroll.LineLeft();
                    this.RightCanvas.Visibility = 0;
                    this.LeftCanvas.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("DarkOrange"));
                    this.RightCanvas.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF414141"));
                    double d1 = scroll.HorizontalOffset;
                    double d2 = scroll.ViewportWidth;
                    double d3 = scroll.ExtentWidth;
                    if (d1 == 0)
                    {
                        this.LeftCanvas.Visibility = (Visibility)1;
                    }
                }
                scroll.ScrollToTop();
            }
        }
        #endregion

        #region 初始化数据
        void InitData()
        {
            YearWidth = 170;
            MonthWidth = 150;
            double k = _Width;
            if (k < 1000.00)
            {
                YearWidth = 85;
                MonthWidth = 75;
            }
            if (k > 2800.00)
            {
                YearWidth = 250;
                MonthWidth = 225;
            }

            MonthList = new ObservableCollection<Date>()
            {
                 new Date{Time_Months="一 月",ID=1,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="二 月",ID=2,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="三 月",ID=3,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="四 月",ID=4,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="五 月",ID=5,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="六 月",ID=6,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="七 月",ID=7,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="八 月",ID=8,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="九 月",ID=9,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="十 月",ID=10,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="十一月",ID=11,ItemWidthMonth=MonthWidth,IsSelect=false},
                 new Date{Time_Months="十二月",ID=12,ItemWidthMonth=MonthWidth,IsSelect=false}
            };
            YearNow = DateTime.Now.Year;
            YearList = new ObservableCollection<Date>() { };
            for (int i = 29; i >= 0; i--)
            {
                YearList.Add(new Date { Time_Years = YearNow - i, ID = 30 - i, ItemWidthYear = YearWidth, IsSelect = false });
            }
            this.listView_Months.ItemsSource = MonthList; 
            this.listView_Years.ItemsSource = YearList;
            //滚动条设置到底部
            this.listView_Years.ScrollIntoView(this.listView_Years.Items[29]);
            //默认选择当前年份
            this.listView_Years.SelectedIndex = 29;
        }
        #endregion

        #region 查询数据
        public   List<Date> Select_Years = new List<Date>();
        public   List<Date> Select_Months = new List<Date>();
        public void Search_Data()
        {

           
            if (listView_Years.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择年份 ！");
            }
            else
            {
                if (listView_Months.SelectedItems.Count == 0)   
                {
                    foreach (Date item in listView_Years.SelectedItems)
                    {
                        Select_Years.Add(item);
                        MessageBox.Show(item.Time_Years.ToString()+" 全年");
                    }
                }
                else
                {
                    foreach (Date item in listView_Years.SelectedItems)
                    {
                        Select_Years.Add(item);
                        MessageBox.Show(item.Time_Years.ToString());
                    }
                    foreach (Date item in listView_Months.SelectedItems)
                    {
                        Select_Months.Add(item);
                        MessageBox.Show(item.Time_Months);
                    }
                }
            }
        }
        #endregion

        #region 时间轴选择操作

        /// <summary>
        /// 年份拖动连选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static bool IsClickYears = true;
        public int startID_Year { get; set; }
        public int startID_Year1 { get; set; }
        public int endID_Year { get; set; }
        private void Start_Years_MouseDown(object sender, MouseButtonEventArgs e)
        {
            listView_Months.SelectedItems.Clear();   
            listView_Years.SelectedItems.Clear();
            IsClickYears = false;
            Label label = sender as Label;
            startID_Year1 = int.Parse(label.Tag.ToString());
            MaxID_Year = MinID_Year = startID_Year1;
        }
        private void End_Years_MouseUp(object sender, MouseButtonEventArgs e)
        {
            IsClickYears = true;

            Date selectitem = new Date();
            selectitem = listView_Years.SelectedItem as Date;
            startID_Year = selectitem.ID;

            Label label = sender as Label;
            endID_Year = int.Parse(label.Tag.ToString());

            if (startID_Year < endID_Year)
            {
                foreach (Date item in listView_Years.Items)
                {
                    if (item.ID >= startID_Year && item.ID <= endID_Year)
                    {
                        YearList[item.ID - 1].IsSelect = true;
                    }
                    else
                    {
                        YearList[item.ID - 1].IsSelect = false;
                    }
                }
                this.listView_Years.ItemsSource = YearList;
            }
            if (startID_Year > endID_Year)
            {
                foreach (Date item in listView_Years.Items)
                {
                    if (item.ID <= startID_Year && item.ID >= endID_Year)
                    {
                        YearList[item.ID - 1].IsSelect = true;
                    }
                    else
                    {
                        YearList[item.ID - 1].IsSelect = false;
                    }
                }
                this.listView_Years.ItemsSource = YearList;
            }

            Search_Data();
        }

        /// <summary>
        /// 月份拖动连选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static bool IsClickMonth = true;
        public int startID_Month { get; set; }
        public int endID_Month { get; set; }
        public int startID_Month1 { get; set; }
        private void Start_Months_MouseDown(object sender, MouseButtonEventArgs e)
        {
            listView_Months.SelectedItems.Clear();
            IsClickMonth = false;
            Label label = sender as Label;
            startID_Month1 = int.Parse(label.Tag.ToString());
            MaxID_Month = MinID_Month = startID_Month1;

        }
        private void End_Months_MouseUp(object sender, MouseButtonEventArgs e)
        {
            IsClickMonth = true;

            Date selectitem = new Date();
            selectitem = listView_Months.SelectedItem as Date;
            startID_Month = selectitem.ID;

            Label label = sender as Label;
            endID_Month = int.Parse(label.Tag.ToString());

            if (startID_Month < endID_Month)   
            {
                foreach (Date item in listView_Months.Items)
                {
                    if (item.ID >= startID_Month && item.ID <= endID_Month)
                    {
                        MonthList[item.ID - 1].IsSelect = true;
                    }
                    else
                    {
                        MonthList[item.ID - 1].IsSelect = false;
                    }
                    this.listView_Months.ItemsSource = MonthList;
                }
            }
            if (startID_Month > endID_Month)
            {
                foreach (Date item in listView_Months.Items)
                {
                    if (item.ID <= startID_Month && item.ID >= endID_Month)
                    {
                        MonthList[item.ID - 1].IsSelect = true;
                    }
                    else
                    {
                        MonthList[item.ID - 1].IsSelect = false;
                    }
                    this.listView_Months.ItemsSource = MonthList;
                }

            }

            Search_Data();
        }

        /// <summary>
        /// 拖选时，连续选择，退回可撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param
        public int MaxID_Month { get; set; }
        public int MinID_Month { get; set; }
        public int MaxID_Year { get; set; }
        public int MinID_Year { get; set; }

        private void Month_MouseEnter(object sender, MouseEventArgs e)
        {
            listView_Months.Focus();   
            if (IsClickMonth == false)
            {
                Label label = sender as Label;
                int ID = int.Parse(label.Tag.ToString());
                if (ID > startID_Month1)
                {
                    if (ID > MaxID_Month)
                    {
                        MaxID_Month = ID;
                    }
                    if (ID < MaxID_Month)
                    {
                        MonthList[MaxID_Month - 1].IsSelect = false;
                        this.listView_Months.ItemsSource = MonthList; 
                    }
                }
                if (ID < startID_Month1)
                {
                    if (ID < MinID_Month)
                    {
                        MinID_Month = ID;
                    }
                    if (ID > MinID_Month)
                    {
                        MonthList[MinID_Month - 1].IsSelect = false;
                        this.listView_Months.ItemsSource = MonthList; 
                    }
                }

                if (ID != startID_Month1)
                {
                    MonthList[ID - 1].IsSelect = !MonthList[ID - 1].IsSelect;
                    this.listView_Months.ItemsSource = MonthList;    
                }
            }
        }

        private void Years_MouseEnter(object sender, MouseEventArgs e)
        {
            listView_Years.Focus();
            if (IsClickYears == false)
            {
                Label label = sender as Label;
                int ID = int.Parse(label.Tag.ToString());
                if (ID > startID_Year1)
                {
                    if (ID > MaxID_Year)
                    {
                        MaxID_Year = ID;
                    }
                    if (ID < MaxID_Year)
                    {
                        YearList[MaxID_Year - 1].IsSelect = false;
                        this.listView_Years.ItemsSource = YearList;
                    }
                }
                if (ID < startID_Year1)
                {
                    if (ID < MinID_Year)
                    {
                        MinID_Year = ID;
                    }
                    if (ID > MinID_Year)
                    {
                        YearList[MinID_Year - 1].IsSelect = false;
                        this.listView_Years.ItemsSource = YearList;
                    }
                }

                if (ID != startID_Year1)
                {
                    YearList[ID - 1].IsSelect = !YearList[ID - 1].IsSelect;
                    this.listView_Years.ItemsSource = YearList;
                }
            }
        }
        #endregion
    }

        /// <summary>
    /// 时间类
    /// </summary>
    public class Date : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
        /// <summary>
        /// 月份
        /// </summary>
        private string timeMonths;
        public string Time_Months
        {
            get { return timeMonths; }
            set
            {
                timeMonths = value;
                //RaisePropertyChanged("Time_Months");
                OnPropertyChanged(new PropertyChangedEventArgs("Time_Months"));
            }
        }
        /// <summary>
        /// 年份
        /// </summary>
        private int timeYears;
        public int Time_Years
        {
            get { return timeYears; }
            set
            {
                timeYears = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Time_Years"));

               // RaisePropertyChanged("Time_Years");
            }
        }
        /// <summary>
        /// ID
        /// </summary>
        private int id;
        public int ID
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ID"));

            }
        }

        /// <summary>
        /// ID
        /// </summary>
        private bool isSelect;
        public bool IsSelect
        {
            get { return isSelect; }
            set
            {
                isSelect = value;
                OnPropertyChanged(new PropertyChangedEventArgs("IsSelect"));

                //RaisePropertyChanged("IsSelect");
            }
        }

        public static int itemWidthMonth;
        public int ItemWidthMonth
        {
            get { return itemWidthMonth; }
            set
            {
                itemWidthMonth = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ItemWidthMonth"));

               // RaisePropertyChanged("ItemWidthMonth");
            }
        }
        public static int itemWidthYear;
        public int ItemWidthYear
        {
            get { return itemWidthYear; }
            set
            {
                itemWidthYear = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ItemWidthYear"));

             //   RaisePropertyChanged("ItemWidthYear");
            }
        }
    }

}
